// pages/search/searchGoods/searchGoods.js
let app = getApp();
import {
	list
} from '../../../../api/product'
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		brandList:{
			type:Array
		},
		min:{
			type:String
		},
		max:{
			type:String
		},
		goods: {
			type: String
		},
		searchList: {
			type: Array
		}
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		isNull:false,
		brands: [],
		isScreen: false,
		searchBrand: [],
		minPrice: '',
		isShow: true,
		maxPrice: '',
		products: [],
		category: {
			categoryTitle: '品牌推荐',
			categoryList: []
		}

	},
	lifetimes: {
		
		attached() {
			console.log(this.data.brandList)
			this.data.maxPrice=this.data.max
			this.data.minPrice=this.data.min
			this.load()
			this.data.category.categoryList=this.data.brandList
			this.setData({
				maxPrice:this.data.maxPrice,
				minPrice:this.data.minPrice,
				category: this.data.category
			})
		}
	},
	created() {

	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		itemClick(e) { // 点击
			this.data.category.categoryList.forEach(item=>{
				item.isActive=false
			})
			this.data.category.categoryList[e.currentTarget.dataset.index].isActive=true
			
			this.setData({
				category:this.data.category
			})
			console.log(this.data.category)
		},
		reset() { // 重置
			this.data.minPrice=''
			this.data.maxPrice=''
			this.data.category.categoryList.forEach((item, i) => {
				let setActive = `category.categoryList[${i}].isActive`
				this.setData({
					minPrice:this.data.minPrice,
					maxPrice:this.data.maxPrice,
					[setActive]: false
				})
			})

		},
		searchMax(e) {
			this.data.maxPrice = e.detail.value
			this.setData({
				maxPrice: this.data.maxPrice
			})
		},
		searchMin(e) {
			this.data.minPrice = e.detail.value
			this.setData({
				minPrice: this.data.minPrice
			})
		},

		enterClick() {
			this.data.category.categoryList.forEach(item => {
				if (item.isActive == true) {
					this.data.searchBrand.push(item.brandId)
				}
				this.setData({
					searchBrand: this.data.searchBrand
				})
			})
			if (this.data.searchBrand.length > 0 || this.data.maxPrice.length > 0 || this.data.minPrice.length > 0) {
				this.data.isScreen = true
			}
			list({
				brandId: this.data.searchBrand,
				name: this.data.goods,
				startPrice: this.data.minPrice,
				endPrice: this.data.maxPrice,
				mchId: app.globalData.mchId
			}).then(res => {
				this.data.isShow = false
				this.data.products = res.data.data
				if(this.data.products.length==0){
						this.data.isNull=true
				}
				this.setData({
					isNull:this.data.isNull,
					isScreen: this.data.isScreen,
					products: this.data.products,
					isShow: this.data.isShow
				})
				this.triggerEvent('loadProduct', {
					isNull:this.data.isNull,
					products: this.data.products,
					isShow: this.data.isShow,
					isScreen: this.data.isScreen,
					brandId:this.data.searchBrand,
					maxPrice:this.data.maxPrice,
					minPrice:this.data.minPrice,
					brandList:this.data.brandList
				})
			})

		},

		load() {
			this.setData({
				statusBarHeight: app.globalData.statusBarHeight,
			})
		},
		mask() { // 退出
			this.triggerEvent('mask', {
				sClick: false
			}, {})
		}
	}
})